﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtilsDll.Utils.Currency.WebUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;

namespace Telegram.UserTool.TelegramInfo
{
    internal class TelegramHelper
    {
        /// <summary>
        /// 发送一条信息
        /// </summary>
        /// <param name="botClient"></param>
        /// <param name="Message"></param>
        /// <param name="chatId">聊天ID</param>
        /// <returns></returns>
        public static async Task SendMessageAsync(ITelegramBotClient botClient, string Message, ChatId chatId)
        {
            try
            {
                await botClient.SendTextMessageAsync(
                                      chatId: chatId,
                                      text: "请您发送任意表情包给我，我会自动下载，谢谢！"
                                    );
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 下载表情表合集
        /// </summary>
        /// <param name="StickerInfo"></param>
        /// <param name="DownloadFloder"></param>
        /// <returns></returns>
        public static async Task DownloadStickers(ITelegramBotClient botClient, Message StickerInfo, string DownloadFloder, string BotToken)
        {
            await DownloadStickers(botClient, StickerInfo.Sticker.SetName, DownloadFloder, BotToken);
        }

        /// <summary>
        /// 下载表情包合集
        /// </summary>
        /// <param name="StickerName"></param>
        /// <param name="DownloadFloder"></param>
        /// <returns></returns>
        public static async Task DownloadStickers(ITelegramBotClient botClient, string StickerName, string DownloadFloder, string BotToken)
        {
            StickerSet sticker = await botClient.GetStickerSetAsync(StickerName);
            foreach (var item in sticker.Stickers)
            {
                await DownloadSticker(botClient, DownloadFloder, item, BotToken);
            }
        }

        /// <summary>
        /// 下载单个表情
        /// </summary>
        /// <param name="DownloadFloder"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public static async Task DownloadSticker(ITelegramBotClient botClient, string DownloadFloder, Sticker item, string BotToken)
        {
            Bot.Types.File Stcikesfile = await botClient.GetFileAsync(item.FileId);

            new WebHelper().DownloadToFile($"https://api.telegram.org/file/bot{BotToken}/{Stcikesfile.FilePath}",
                DownloadFloder + $"Emoji：{Transcoding.UrlEncode(item.Emoji).Replace("%", "-")}：End"
                + FileNameHelper.GetFileName(Stcikesfile.FilePath)
               );
        }
    }
}